package com.itheima.mapper;

import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DeptMapper {
    /**
     * 查询所有部门数据
     */
    /*
    方案1手动结果映射
    @Results({
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
     */

    /*
    方案2使用别名
     */
    @Select("select id, name, create_time as createTime, update_time as updateTime from dept order by update_time desc ;")
    List<Dept> findALL();

    /**
     * 通过id删除部门数据
     */
    @Delete("delete from dept where id=#{id}")
    void deleteById(Integer id);

    /**
     * 新增部门
     */
    @Insert("insert into dept(name,create_time,update_time) values(#{name},#{createTime},#{updateTime})")
    void insert(Dept dept);

    /**
     * 查询部门通过id
     */
    @Select("select id, name, create_time , update_time  from dept where id=#{id}")
    Dept findById(Integer id);


    /**
     * 修改部门通过id
     */
    @Update("update dept set name=#{name},update_time=#{updateTime} where id=#{id}")
    void update(Dept dept);

    /**
     * 通过id获取部门人数
     * @param id
     * @return
     */
    @Select("select d.id,count(*) from dept d join emp e on e.dept_id =d.id where d.id=#{id};")
    Integer getCountByDeptId(Integer id);
}
